package com.example.my_self_view.ratingbar

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.example.my_self_view.R
import kotlin.math.max

/**
 * @dsc     这种方法的 drawable 仅限于 图片格式，如png、jpg等
 * @Author  LiWeiZhong
 * @Date    2025/1/17 9:20
 * @Version 1.0
 */
class RatingBar1: View {

    private var gradeNumber: Int = 5
    private var imgSelected: Bitmap? = null
    private var imgUnselected: Bitmap? = null

    private val intervalDip: Int = 3

    constructor(context: Context): this(context, null) {}
    constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0) {}
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): this(context, attrs, defStyleAttr, 0) {}
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int): super(context, attrs, defStyleAttr, defStyleRes) {
        initAttrs(context, attrs)
    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatingBar1)
        typedArray.apply {
            try {
                gradeNumber = getInt(R.styleable.RatingBar1_gradeNumber1, gradeNumber)
                val selectedId = getResourceId(R.styleable.RatingBar1_imgSelected1, -1)
                imgSelected = BitmapFactory.decodeResource(context.resources,
                    if (selectedId != -1) selectedId else R.drawable.star_selected
                )
                val unselectedId = getResourceId(R.styleable.RatingBar1_imgUnselected1, -1)
                imgUnselected = BitmapFactory.decodeResource(context.resources,
                    if (selectedId != -1) unselectedId else R.drawable.star_unselected
                )
            } finally {
                recycle()
            }
        }
    }

    fun dipToPx(value: Float): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)

        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)

        if (widthMode == MeasureSpec.AT_MOST) {
            var contentSelectedWidth = 0
            var contentUnselectedWidth = 0
            var contentWidth = 0
            if (imgSelected != null) contentSelectedWidth = imgSelected!!.width
            if (imgUnselected != null) contentUnselectedWidth = imgUnselected!!.width
            contentWidth = max(contentSelectedWidth, contentUnselectedWidth)
            width = contentWidth * 5 + (dipToPx(intervalDip.toFloat()) * 4).toInt() + paddingStart + paddingEnd
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            var contentSelectedHeight = 0
            var contentUnselectedHeight = 0
            var contentHeight = 0
            if (imgSelected != null) contentSelectedHeight = imgSelected!!.height
            if (imgUnselected != null) contentUnselectedHeight = imgUnselected!!.height
            contentHeight = max(contentSelectedHeight, contentUnselectedHeight)
            height = contentHeight + paddingTop + paddingBottom
        }

        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas) {
        imgUnselected?.apply {
            for (i in 0 until 1) {
                canvas.drawBitmap(imgUnselected!!,  i * (imgUnselected!!.width + dipToPx(intervalDip.toFloat())), 0f, null)
            }
        }
    }
}